package com.taxi.driver.module.main.mine;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianxx.network.RetrofitRequestTool;
import com.qianxx.utils.SP;
import com.taxi.driver.R;
import com.taxi.driver.common.AppConfig;
import com.taxi.driver.common.BaseFragment;
import com.taxi.driver.config.DriverType;
import com.taxi.driver.data.entity.GovernEntity;
import com.taxi.driver.event.MsgEvent;
import com.taxi.driver.module.main.mine.dagger.DaggerMineComponent;
import com.taxi.driver.module.main.mine.dagger.MineModule;
import com.taxi.driver.module.vo.MineVO;
import com.taxi.driver.module.web.ShareActivity;
import com.taxi.driver.util.Navigate;
import com.taxi.driver.widget.CircleImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：我
 */
public class MineFragment extends BaseFragment implements MineContract.View {

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @BindView(R.id.iv_driver_avatar)
    CircleImageView mIvDriverAvatar;
    @BindView(R.id.tv_driver_name)
    TextView mTvDriverName;
    @BindView(R.id.tv_licence_plate)
    TextView mTvLicencePlate;
    @BindView(R.id.tv_car_type)
    TextView mTvCarType;
    @BindView(R.id.rl_my_wallet)
    RelativeLayout mRlMyWallet;
    @BindView(R.id.rl_passengers_evaluation)
    RelativeLayout mRlPassengersEvaluation;
    @BindView(R.id.rl_assess_statistical)
    RelativeLayout mRlAssessStatistical;
    @BindView(R.id.rl_message_center)
    RelativeLayout mRlMessageCenter;
    @BindView(R.id.rl_helper_center)
    RelativeLayout mRlHelperCenter;
    @BindView(R.id.rl_setting)
    RelativeLayout mRlSetting;
    @BindView(R.id.tv_evaluation)
    TextView mTvEvaluation;
    @BindView(R.id.rl_share)
    RelativeLayout mRlShare;
    @BindView(R.id.tv_share)
    TextView mTvShare;
    @BindView(R.id.tv_message_unread_count)
    TextView mTvUnreadCount;


    @Inject
    MinePresenter mPresenter;

    private SP mSP;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, mView);

        /* 跨城拼车，暂时先隐藏"考核统计" TODO */
        mRlAssessStatistical.setVisibility(AppConfig.isCarpool() ? View.GONE : View.VISIBLE);

        if ( mPresenter.getDriverType() == DriverType.SPECIAL) {
            mTvEvaluation.setText(R.string.passengers_evaluation);
        }

        return mView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        DaggerMineComponent.builder()
                .appComponent(getAppComponent())
                .mineModule(new MineModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        mPresenter.subscribe();
        mPresenter.getInvite();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            mPresenter.getInvite();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.unsubscribe();
    }

    @Override
    public void showDriverInfo(MineVO vo) {
        Glide.with(this).load(vo.driverAvatar).into(mIvDriverAvatar);
        mTvDriverName.setText(vo.driverName);
        if (!TextUtils.isEmpty(vo.licencePlate)) {
            mTvLicencePlate.setVisibility(View.VISIBLE);
            mTvLicencePlate.setText(vo.licencePlate);
        } else {
            mTvLicencePlate.setVisibility(View.GONE);
            mTvLicencePlate.setBackgroundColor(getResources().getColor(R.color.transparent));
            mTvLicencePlate.setText("");
        }

         if (mPresenter.getDriverType() == DriverType.SPECIAL ) {
            mTvCarType.setText((TextUtils.isEmpty(vo.carLevelName) ? "" : vo.carLevelName)
                    + " " + (TextUtils.isEmpty(vo.carColor) ? "" : vo.carColor)
                    + "·" + (TextUtils.isEmpty(vo.brandName) ? "" : vo.brandName));
        }
    }

    @Override
    public void setMessageLogo(GovernEntity.MessageBean bean) {
        int count = bean.getUnreadCount();
        if (count == 0) {
            mTvUnreadCount.setVisibility(View.GONE);
        } else {
            mTvUnreadCount.setVisibility(View.VISIBLE);
            if (count < 100) {
                mTvUnreadCount.setText(count + "");
            } else {
                mTvUnreadCount.setText("99+");
            }
        }
    }

    @Override
    public void invite(GovernEntity.InviteBean bean) {
        boolean isInvite = bean.isIsOpen();
        if (isInvite) {
            mRlShare.setVisibility(View.VISIBLE);
        } else {
            mRlShare.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.rl_my_wallet, R.id.rl_passengers_evaluation, R.id.rl_assess_statistical,
            R.id.rl_share, R.id.rl_message_center, R.id.rl_helper_center, R.id.rl_setting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_my_wallet:
                Navigate.openMyWallet(getContext());
                break;
            case R.id.rl_passengers_evaluation:
                Navigate.openEvaluation(getContext());
                break;
            case R.id.rl_assess_statistical:
                Navigate.openAssenssmentStatisitcal(getContext());
                break;
            case R.id.rl_share:
                String url = AppConfig.HOST
                        + "/yue/share_driver"
                        + "?appid=" + getString(R.string.yueyue_appkey)
                        + "&token=" + RetrofitRequestTool.getToken(mSP);
                ShareActivity.actionStart(getContext(), url);
                break;
            case R.id.rl_message_center:
                Navigate.openMessageCenter(getContext());
                break;
            case R.id.rl_helper_center:
                Navigate.openHelpCenter(getContext());
                break;
            case R.id.rl_setting:
                Navigate.openSetting(getContext());
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(MsgEvent event) {
        switch (event.type) {
            case MsgEvent.SYSTEM_MESSAGE:
            case MsgEvent.REFRESH_STATUS:
                mPresenter.getInvite();
                break;
        }
    }
}
